<?php

class AdminerController extends BaseController
{
	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new Adminer;

		// Uncomment the following line if AJAX validation is needed
		 $this->performAjaxValidation($model);

		if(isset($_POST['Adminer']))
		{
			$model->attributes=$_POST['Adminer'];
			if($model->save()){
				//成功跳转
				$this->redirect(array('admin'));
			}
		}
		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);
		
		//只有超级管理员才能修改超级管理员
		if($id==1&&Yii::app()->user->getId()!=1){
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		}
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Adminer']))
		{
			$model->attributes=$_POST['Adminer'];
			if($model->save()){
				//成功跳转
				$this->redirect(array('admin'));
			}
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete()
	{
		//操作ID号格式处理
		if(isset($_GET['id'])){
			$id = $_GET['id'];
		}elseif(isset($_POST['ids'])){
			$id =$_POST['ids'];
		}else{
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		}
		//超级管理员不能删除
		if(in_array(1,$id)){
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		}
		
		$id = is_array($id)?join(',',$id):(int)$id;
		//处理条件
		$criteria=new CDbCriteria;
		$criteria->addCondition("zda_id in ($id)");
		//执行操作
		if(Adminer::model()->deleteAll($criteria)){
			//成功跳转
			$this->redirect(Yii::app()->request->urlReferrer);
		}
	}


	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new Adminer('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Adminer']))
			$model->attributes=$_GET['Adminer'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Adminer::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='adminer-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
	/**
	 * 后台登录页面
	 * */
	public function actionLogin(){
		$this->layout = "//layouts/manage_mini_foot";
		$model=new AdminLogin;
		
		//ajax验证
		if(isset($_POST['ajax']) && $_POST['ajax']==='adminlogin-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
		
		if(isset($_POST['AdminLogin']))
		{
			$model->attributes=$_POST['AdminLogin'];
			if($model->validate() && $model->login()){
				//保存登录信息
				Yii::app()->user->setState('userid','0',1);
				//更新登录记录
				Adminer::model()->updateLoginInfo();
				$this->redirect(Yii::app()->user->returnUrl);
			}else{
				//清空密码,验证码
				$model->zda_password = "";
				$model->verifyCode = "";
			}
		}
		$this->render('login',array(
				'model'=>$model,
		));
	}
	
	/**
	 * 重置员工密码
	 */
	public function actionResetPassword(){
		$id = Yii::app()->user->getId();
		$model = Adminer::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		
		//场景设为重置密码
		$model->scenario = "update_password";
		//保存信息
		if(isset($_POST['Adminer']))
		{
			//验证旧密码
			if($model->zda_password!=$model->passwordEncode($_POST['Adminer']['old_password'])){
				//失败跳转
				$this->render('../index/error',array('message'=>'对不起，旧密码错误，请核对后再试！',));
				exit;
			}
			
			$model->attributes=$_POST['Adminer'];
			$model->zda_password = $model->passwordEncode($model->zda_password);
			$model->re_password = $model->passwordEncode($model->re_password);
			//删除子账户
			if($model->save()){
				//成功跳转
				$this->redirect(array($this->pre_module_url.'/memSub/account'));
			}
		}
		
		//将密码置空
		$model->zda_password = "";
	
		//渲染视图
		$this->render('../memSub/reset_password',array(
				'model'=>$model,
		));
	}
	
	//验证码
	public function actions()
	{
		return array(
				// captcha action renders the CAPTCHA image displayed on the contact page
				'captcha'=>array(
						'class'=>'CCaptchaAction',
						'backColor'=>0xFFFFFF,  //背景颜色
						'minLength'=>4,  //最短为4位
						'maxLength'=>4,   //是长为4位
						'transparent'=>true,  //显示为透明
				),
		);
	}
	/**
	 * 退出
	 */
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}
}
